package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HmzRechargeRecords;
import com.ruoyi.system.mapper.HmzRechargeRecordsMapper;
import org.apache.poi.hpsf.Decimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HmzEmployeeInfoMapper;
import com.ruoyi.system.domain.HmzEmployeeInfo;
import com.ruoyi.system.service.IHmzEmployeeInfoService;

/**
 * 员工信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-30
 */
@Service
public class HmzEmployeeInfoServiceImpl implements IHmzEmployeeInfoService
{
    @Autowired
    private HmzEmployeeInfoMapper hmzEmployeeInfoMapper;
    @Autowired
    private HmzRechargeRecordsMapper hmzRechargeRecordsMapper;

    /**
     * 查询员工信息
     *
     * @param id 员工信息主键
     * @return 员工信息
     */
    @Override
    public HmzEmployeeInfo selectHmzEmployeeInfoById(Long id)
    {
        return hmzEmployeeInfoMapper.selectHmzEmployeeInfoById(id);
    }
    /**
     * 查询员工信息
     *
     * @param name 员工信息名称
     * @return 员工信息
     */
    @Override
    public HmzEmployeeInfo selectHmzEmployeeInfoByName(String name)
    {
        return hmzEmployeeInfoMapper.selectHmzEmployeeInfoByName(name);
    }

    /**
     * 查询员工信息列表
     *
     * @param hmzEmployeeInfo 员工信息
     * @return 员工信息
     */
    @Override
    public List<HmzEmployeeInfo> selectHmzEmployeeInfoList(HmzEmployeeInfo hmzEmployeeInfo)
    {
        List<HmzEmployeeInfo> hmzEmployeeInfos = hmzEmployeeInfoMapper.selectHmzEmployeeInfoList(hmzEmployeeInfo);
        if(!hmzEmployeeInfos.isEmpty()){
            for (HmzEmployeeInfo employeeInfo : hmzEmployeeInfos) {
                Long id = employeeInfo.getId();
                if(hmzEmployeeInfo.getMonth() !=null){
                    Double money = hmzEmployeeInfoMapper.selectMeoneyByemId(id,hmzEmployeeInfo.getMonth());
                    employeeInfo.setMoney(money);
                }
                Long selectznums = hmzEmployeeInfoMapper.selectznums(id);
                Long selectwnums = hmzEmployeeInfoMapper.selectwnums(id);
                Long selectynums = hmzEmployeeInfoMapper.selectynums(id);

                employeeInfo.setZnums(selectznums);
                employeeInfo.setWnums(selectwnums);
                employeeInfo.setYnums(selectynums);
            }
        }
        return hmzEmployeeInfos;
    }

    /**
     * 新增员工信息
     *
     * @param hmzEmployeeInfo 员工信息
     * @return 结果
     */
    @Override
    public int insertHmzEmployeeInfo(HmzEmployeeInfo hmzEmployeeInfo)
    {
        hmzEmployeeInfo.setCreateTime(DateUtils.getNowDate());
        return hmzEmployeeInfoMapper.insertHmzEmployeeInfo(hmzEmployeeInfo);
    }

    /**
     * 修改员工信息
     *
     * @param hmzEmployeeInfo 员工信息
     * @return 结果
     */
    @Override
    public int updateHmzEmployeeInfo(HmzEmployeeInfo hmzEmployeeInfo)
    {
        hmzEmployeeInfo.setUpdateTime(DateUtils.getNowDate());
//        插入修改记录到充值记录里
        HmzRechargeRecords hmzRechargeRecords = new HmzRechargeRecords();
//        赋值充值人id
        hmzRechargeRecords.setEmployeeId(hmzEmployeeInfo.getId());
//        赋值充值金额(传递的充值金额-当前余额)
//        查询此员工的当前余额信息
        BigDecimal balance = hmzEmployeeInfoMapper.selectHmzEmployeeInfoById(hmzEmployeeInfo.getId()).getBalance();
        BigDecimal difference = hmzEmployeeInfo.getBalance().subtract(balance);
        hmzRechargeRecords.setMoney(difference);
//        赋值当前余额
        hmzRechargeRecords.setBalance(hmzEmployeeInfo.getBalance());
//        赋值原余额
        hmzRechargeRecords.setOldBalance(balance);
////        赋值充值时间
        hmzRechargeRecords.setCreateTime(DateUtils.getNowDate());
        hmzRechargeRecordsMapper.insertHmzRechargeRecords(hmzRechargeRecords);
        return hmzEmployeeInfoMapper.updateHmzEmployeeInfo(hmzEmployeeInfo);
    }

    /**
     * 批量删除员工信息
     *
     * @param ids 需要删除的员工信息主键
     * @return 结果
     */
    @Override
    public int deleteHmzEmployeeInfoByIds(Long[] ids)
    {
        return hmzEmployeeInfoMapper.deleteHmzEmployeeInfoByIds(ids);
    }

    /**
     * 删除员工信息信息
     *
     * @param id 员工信息主键
     * @return 结果
     */
    @Override
    public int deleteHmzEmployeeInfoById(Long id)
    {
        return hmzEmployeeInfoMapper.deleteHmzEmployeeInfoById(id);
    }

    @Override
    public int updateBalance(Long employeeId, BigDecimal balance) {
        return hmzEmployeeInfoMapper.updateBalance(employeeId,balance);
    }

    @Override
    public int updateBalanceNew(Long employeeId, BigDecimal balance) {
        return hmzEmployeeInfoMapper.updateBalanceNew(employeeId,balance);
    }

    @Override
    public BigDecimal selectPrice() {
        return hmzEmployeeInfoMapper.selectPrice();
    }

    @Override
    public HmzEmployeeInfo selectByEpc(String epc) {
        HmzEmployeeInfo hmzEmployeeInfo = hmzEmployeeInfoMapper.selectByEpc(epc);
        List<HmzEmployeeInfo> hmzEmployeeInfos = hmzEmployeeInfoMapper.selectHmzEmployeeInfoList(hmzEmployeeInfo);
        if(!hmzEmployeeInfos.isEmpty()){
            for (HmzEmployeeInfo employeeInfo : hmzEmployeeInfos) {
                Long id = employeeInfo.getId();
                Long selectznums = hmzEmployeeInfoMapper.selectznums(id);
                Long selectwnums = hmzEmployeeInfoMapper.selectwnums(id);
                Long selectynums = hmzEmployeeInfoMapper.selectynums(id);
                employeeInfo.setZnums(selectznums);
                employeeInfo.setWnums(selectwnums);
                employeeInfo.setYnums(selectynums);
                employeeInfo.setEpc(hmzEmployeeInfo.getEpc());
            }
            return hmzEmployeeInfos.get(0);
        }else {
            HmzEmployeeInfo hmzEmployeeInfo1 = new HmzEmployeeInfo();
            hmzEmployeeInfo1.setZnums(0L);
            hmzEmployeeInfo1.setWnums(0L);
            hmzEmployeeInfo1.setYnums(0L);
            hmzEmployeeInfo1.setEpc("被禁用人员");

          return  hmzEmployeeInfo1;
        }

    }

    @Override
    public int updateIsLoss(HmzEmployeeInfo hmzEmployeeInfo) {
        hmzEmployeeInfo.setUpdateTime(DateUtils.getNowDate());
        return hmzEmployeeInfoMapper.updateHmzEmployeeInfo(hmzEmployeeInfo);
    }
}
